package com.dstz.bpm.engine.parser.node;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ArrayUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dstz.base.core.util.StringUtil;
import com.dstz.bpm.api.engine.plugin.def.BpmDef;
import com.dstz.bpm.api.model.nodedef.Button;
import com.dstz.bpm.api.model.nodedef.impl.BaseBpmNodeDef;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Component;

@Component
public class ButtonsParse extends AbsNodeParse<Button> {
   @Override
   public String getKey() {
      return "btnList";
   }

   @Override
   public void setDefParam(BaseBpmNodeDef userNodeDef, Object object) {
      List<Button> btnList = (List)object;
      userNodeDef.setButtons(btnList);
   }

   @Override
   public boolean isArray() {
      return true;
   }

   @Override
   public void JSONAmend(BaseBpmNodeDef userNodeDef, Object object, JSON thisJson) {
      JSONObject jsonConfig = (JSONObject)thisJson;
      if (this.isEmpty(object)) {
         jsonConfig.put("btnList", JSON.toJSON(userNodeDef.getButtons()));
      }

   }

   private boolean isEmpty(Object object) {
      if (object == null) {
         return true;
      } else if (object instanceof String) {
         return StringUtil.isEmpty((String)object);
      } else if (object instanceof Collection) {
         return CollectionUtil.isEmpty((Collection)object);
      } else if (object.getClass().isArray()) {
         return ArrayUtil.isEmpty((Object[])((Object[])object));
      } else if (object instanceof Map) {
         return MapUtil.isEmpty((Map)object);
      } else if (object instanceof JSON) {
         return ((JSONObject)object).isEmpty();
      } else {
         return object instanceof JSONArray ? ((JSONArray)object).isEmpty() : false;
      }
   }
}
